/*
 * ============================================================================
 *
 *       Filename:  randomarr.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2015年03月07日 10时26分17秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * ============================================================================
 */
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include "randomarr.h"
#include "chap.h"

static int offset = 0;
struct mac_random_map_t random_map[LOCAL_AC_MAX] = {{0}};

uint32_t new_random(unsigned char *mac)
{
	uint32_t random;
	random = chap_get_random();
	random_map[offset].random = random;
	memcpy(random_map[offset].mac, mac, ETH_ALEN);
	offset = (offset + 1) % LOCAL_AC_MAX;
	return random;
}

uint32_t get_random(unsigned char *mac)
{
	int i, j = 0;
	i = (offset + LOCAL_AC_MAX - 1) % LOCAL_AC_MAX;
	/* offset should be check first */
	for(; j < LOCAL_AC_MAX; i++, j++) {
		if(!memcmp(mac, random_map[i % LOCAL_AC_MAX].mac, ETH_ALEN))
			return random_map[i % LOCAL_AC_MAX].random;
	}
	return 0;
}
